package com.cy.service.impl;

import com.cy.common.exception.ServiceException;
import com.cy.common.pojo.CheckBox;
import com.cy.dao.SysRoleDao;
import com.cy.dao.SysRoleMenuDao;
import com.cy.pojo.SysRole;
import com.cy.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    private SysRoleDao SysRoleDao;
    @Autowired
    private SysRoleMenuDao sysRoleMenuDao;

    @Override
    public List<SysRole> selectSysRole(String roleName) {
        return SysRoleDao.selectSysRole(roleName);
    }

    @Override
    public int deleteSysRole(Integer id) {
        int row = SysRoleDao.deleteSysRole(id);
        SysRoleDao.deleteRelationByRoleId(id);
        return row;
    }

    @Override
    public int updateSysRole(SysRole sysRole) {
        int rows = SysRoleDao.updateSysRole(sysRole);
        if(rows==0)
            throw new ServiceException("记录可能已经不存在");
        SysRoleDao.deleteRelationByRoleId(sysRole.getId());
        sysRoleMenuDao.insertRoleMenus(sysRole.getId(),sysRole.getMenuIds());
        return rows;
    }

    @Override
    public int insertSysRole(SysRole sysRole) {
        int rows = SysRoleDao.insertSysRole(sysRole);
        sysRoleMenuDao.insertRoleMenus(sysRole.getId(),sysRole.getMenuIds());
        return rows;
    }

    @Override
    public SysRole findByRoleId(Integer id) {
        return SysRoleDao.findByRoleId(id);
    }

    @Override
    public List<CheckBox> selectCheckRoles() {
        return SysRoleDao.selectCheckRoles();
    }
}
